package com.org.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.org.common.PageBean;
import com.org.common.Result;
import com.org.pojo.Collect;
import com.org.pojo.CommunityBanner;
import com.org.pojo.CommunityMessage;
import com.org.pojo.CommunityTransferCoin;
import com.org.service.ICommunityTransferCoinService;
import com.org.service.NormalUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjx
 * @since 2024-03-03
 */
@RestController
@RequestMapping("/community-transfer-coin")
public class CommunityTransferCoinController {

    @Autowired
    private ICommunityTransferCoinService communityTransferCoinService;

    @Autowired
    private NormalUserService normalUserService;

    // 分页
    @GetMapping("/list")
    public Result<PageBean<CommunityTransferCoin>> getCommunityTransferCoinList(
            Integer pageNum,    //第几页
            Integer pageSize,   //页面容量
            String communityId  // 社区id，可为空，可不传
    ){
        Page<CommunityTransferCoin> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        LambdaQueryWrapper<CommunityTransferCoin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (communityId != null && !communityId.isEmpty()){
            lambdaQueryWrapper.eq(CommunityTransferCoin::getCommunityId, communityId);
        }

        IPage<CommunityTransferCoin> iPage = communityTransferCoinService.page(page, lambdaQueryWrapper);

        List<CommunityTransferCoin> records = iPage.getRecords();
        // 填充用户名
        for (CommunityTransferCoin record : records) {
            String fromName = normalUserService.getById(record.getFromUserId()).getName();
            String toName = normalUserService.getById(record.getToUserId()).getName();
            record.setFromName(fromName);
            record.setToName(toName);
        }

        PageBean<CommunityTransferCoin> pageBean = new PageBean<>();
        pageBean.setTotal(iPage.getTotal());
        pageBean.setItems(records);

        return Result.success(pageBean);
    }


}
